/**
 * Created by Windy on 2018/2/2.
 */
BI.SearchTextValueTrigger = BI.inherit(BI.Trigger, {

    props: function () {
        return {
            baseCls: "bi-search-text-value-trigger",
            height: 24,
            watermark: BI.i18nText("BI-Basic_Search"),
            allowClear: false,
            title: () => this.editor.getText(),
        };
    },

    render: function () {
        var self = this, o = this.options;

        var triggerButton = {
            type: "bi.trigger_icon_button",
            cls: "trigger-icon-button",
            ref: function () {
                self.triggerBtn = this;
            },
            width: o.height,
            height: o.height,
        };

        var stateText = this._digest(o.value, o.items) || o.text;

        return {
            type: "bi.horizontal_fill",
            columnSize: ["fill", 24],
            items: [
                {
                    el: {
                        type: "bi.searcher",
                        ref: function () {
                            self.searcher = this;
                        },
                        isAutoSearch: false,
                        el: {
                            type: "bi.default_text_editor",
                            ref: function () {
                                self.editor = this;
                            },
                            watermark: o.watermark,
                            defaultText: o.defaultText,
                            text: stateText,
                            value: o.value,
                            height: o.height,
                        },
                        popup: {
                            type: "bi.search_text_value_combo_popup",
                            cls: "bi-card",
                            chooseType: BI.ButtonGroup.CHOOSE_TYPE_SINGLE,
                            tipText: BI.i18nText("BI-No_Select"),
                        },
                        onSearch: function (obj, callback) {
                            var keyword = obj.keyword;
                            var finding = BI.Func.getSearchResult(o.items, keyword);
                            var matched = finding.match, find = finding.find;
                            callback(matched, find);
                        },
                        listeners: [{
                            eventName: BI.Searcher.EVENT_CHANGE,
                            action: function () {
                                self.fireEvent(BI.SearchTextValueTrigger.EVENT_CHANGE);
                            }
                        }]
                    }
                }, {
                    el: o.allowClear ? {
                        type: "bi.vertical_adapt",
                        horizontalAlign: "left",
                        scrollable: false,
                        items: [
                            {
                                el: {
                                    type: "bi.icon_button",
                                    ref: function (_ref) {
                                        self.clearBtn = _ref;
                                    },
                                    cls: "close-h-font " + (o.allowClear ? "clear-button" : ""),
                                    stopPropagation: true,
                                    invisible: !BI.isNotEmptyString(stateText),
                                    width: o.height,
                                    height: o.height,
                                    handler: function () {
                                        self.fireEvent(BI.SearchTextValueTrigger.EVENT_CLEAR);
                                    },
                                },
                            }, {
                                el: triggerButton,
                            }
                        ]
                    } : triggerButton,
                    width: 24
                }
            ]
        };
    },

    _setState: function (v) {
        this.editor.setState(v);
    },

    _digest: function (value, items) {
        var result = BI.find(items, function (i, item) {
            return item.value === value;
        });
        return result?.text;
    },

    stopEditing: function () {
        this.searcher.stopSearch();
    },

    getSearcher: function () {
        return this.searcher;
    },

    populate: function (items) {
        this.options.items = items;
    },

    setValue: function (vals) {
        var digestText = this._digest(vals, this.options.items);
        this._setState(digestText);
        this.options.allowClear && this.clearBtn.setVisible(BI.isNotEmptyString(digestText));
    },

    getValue: function () {
        return this.searcher.getValue();
    }
});
BI.SearchTextValueTrigger.EVENT_SEARCHING = "EVENT_SEARCHING";
BI.SearchTextValueTrigger.EVENT_STOP = "EVENT_STOP";
BI.SearchTextValueTrigger.EVENT_START = "EVENT_START";
BI.SearchTextValueTrigger.EVENT_CHANGE = "EVENT_CHANGE";
BI.SearchTextValueTrigger.EVENT_CLEAR = "EVENT_CLEAR";
BI.shortcut("bi.search_text_value_trigger", BI.SearchTextValueTrigger);
